package com.tree.commonutil.csv;

import com.opencsv.CSVReader;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.HeaderColumnNameTranslateMappingStrategy;
import com.tree.commonutil.TbUser;

import java.beans.SimpleBeanInfo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CsvDemo3
 * @Description: TODO
 * @Author: myl
 * @Create_time:2021/5/7 15:30
 */
public class CsvDemo3 {
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream("D:\\document\\download-context\\bop副本.csv"),"gbk"));
        /*
         * 基于列名转换，映射成类
         */
        HeaderColumnNameTranslateMappingStrategy<TbUser> mapper =
                new HeaderColumnNameTranslateMappingStrategy<TbUser>();
        mapper.setType(TbUser.class);

        Map<String,String> columnMapping = new HashMap<String,String>();
        columnMapping.put("Datetime", "dateTime");
        columnMapping.put("Value", "factorValue");
        mapper.setColumnMapping(columnMapping);

        CsvToBean<TbUser> csvToBean = new CsvToBean<TbUser>();

        List<TbUser> list = csvToBean.parse(mapper, reader);

        for(TbUser e : list){
            System.out.println(e);
        }
    }
}
